var scene = null;
var sceneAddress = "http://localhost:8090/iserver/services/realspace-RealHousewsp5/rest/realspace";
//var sceneAddress = "http://localhost:8090/iserver/services/realspace-TEST01/rest/realspace";
//var sceneName ="WholeCacheScene";
var sceneName ="Scene";
var sceneControl = null;
var layer3Ds = null;
var layerName = "House@Model";
var serverAddress="http://localhost:8090/RealHouse/"
//var layerName ="Houses@Test01";
var Hid=null;	
//传入HID，然后将该ID的房屋选中
function addSelect3D(houseid)
{	
	var modelLayer3D=scene.get_layer3Ds().get_item(layerName);
	var select3d=modelLayer3D.get_selection3D();
	select3d.add(houseid);
	//设置样式
	var style3d=new SuperMap.Web.Core.Style3D();
	style3d.set_fill3DMode(3);
	style3d.set_fillForeColor(new SuperMap.Web.Core.Color(255,0,0,100));
	select3d.set_style3D(style3d);
}
function removeSelect3D()
{
	var modelLayer3D=scene.get_layer3Ds().get_item(layerName);
	var select3d=modelLayer3D.get_selection3D();
	select3d.removeAll();
}

function onPageLoad()
{
	var hostUrl = "http://"  + window.location.host;
	//初始化三维场景控件
	try
	{
	    //初始化三维场景控件实例,参数为包含控件的HTML元素
	    sceneControl = new SuperMap.Web.UI.Controls.SceneControl($get("sceneControlDiv"), initCallback, failedCallback);
	    var url = hostUrl + getContextPath() + "/iClient/forRealspace/pluginversion.xml";
	    var updateUrl = hostUrl + sceneControl.checkPluginUpdate(url);
	    if(updateUrl.search(/Realspace/i)!=-1){
	    	document.write("请点击下载 <a href='"+updateUrl +"'>SuperMap iClient For Realspace </a> 更高版本的插件进行安装");
	    }
	}
	catch(e)
    {
        //若没有安装插件，则抛出该异常
        if (e.name == SuperMap.Web.Realspace.ExceptionName.PlugInNotInstalled)
        {
            var url =  hostUrl + getContextPath() + "/iClient/forRealspace/Setup.exe";
            document.write("请点击下载 <a href='"+url+"'>SuperMap iClient For Realspace </a>  插件进行安装");
            return;
        }
        //若使用非IE浏览器，则抛出该异常
        else if (e.name == SuperMap.Web.Realspace.ExceptionName.BrowserNotSupport)
        {
            document.write("<p>SuperMap iClient 6R for Realspace目前只支持IE浏览器</p>");
            return;
        }
        //抛出其他异常
        else
        {
            alert(e.message);
        }
    }
}
//控件初始化完成后的回调函数，初始化完成之后才能进行数据加载
function initCallback()
{
    //获取地球控件场景，控件和场景是一对一的捆绑关系
    scene = sceneControl.get_scene();
	SceneOpen_onclick();
	//sceneControl.removeEvent("objectSelected", handler);
	//sceneControl.addEvent("flownEnd", panAgain);
	//漫游状态
	var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);
	sceneControl.set_sceneAction(panAction); 	
	//ceneControl.addEvent("flownEnd", FlyEnd);

}

//控件初始化失败后的回调函数
function failedCallback()
{
    alert("Realspace initialized failed!");
}
function SceneOpen_onclick()
{
	
	//打开场景	
	scene.open(sceneAddress,sceneName);
	scene.set_name("hello");
	var layer3DServicesList = sceneControl.get_layer3DServicesList();
	var bLoad = layer3DServicesList.load(sceneAddress, sceneName);
	if(bLoad)
	{		
		var layer3DServiceInfo = layer3DServicesList.get_item(layerName);
		var dataName = layer3DServiceInfo.get_dataName();			
		//打开图层
		//var layer3D = scene.get_layer3Ds().add(sceneAddress, layerName, dataName, SuperMap.Web.Realspace.Layer3DType.VECTOR);		
		var layer3D=scene.get_layer3Ds().get_item(layerName);							
		if (layer3D != null) 
		{		
			//获取图层的地理范围，并飞行到该范围
			var geobound = layer3D.get_bounds();   
			scene.get_flyingOperator().flyToBounds(geobound,3000);
		}
	}				
}
function SceneClose_onclick()
{
    //关闭场景
	scene.close();
	scene.refresh();
}
// 获取contextPath
function getContextPath(){
	var urlStr = window.location.href;
	var hostStr = window.location.host;
	var startIndex = urlStr.indexOf(hostStr) + hostStr.length + 1;
	var tmpStr = urlStr.substring(startIndex);
	var lastIndex = tmpStr.indexOf("/");
	var contextPath = "/" + tmpStr.substring(0, lastIndex);
	return contextPath;
}


		
function handler(selection3Ds) 
{	
    //获取选择集的个数	
    var layer3D = scene.get_layer3Ds().get_item(layerName);
	var fieldInfo = "";				
	fieldCount = layer3D.get_fieldInfos().get_count();				
	//清空原来的内容
	jQuery(window.parent.frames["bottomFrame"].document).find("tr").remove();		
	//Hid=layer3D.getFieldValue(0);
	///////**********//////////
	var selectCount = selection3Ds[0].get_count();				 
	var selection3D = selection3Ds[0];
	Hid=selection3D.get_item(0);
	var uid= window.parent.document.frames["login"].session; 
	//将用户查看过的房子添加到user trace的表格中，记录用户查看过的房屋
	//alert(ServletAddress+traceAdd+"HID="+Hid+"&UID="+uid);
	
	
	 
	//通过Post读取数据库中的HouseInfo,然后增加到窗体			 
	jQuery.getJSON(ServletAddress+HouseInfoAddress+"HID="+Hid,function(result){
	 imgurl="";	
	 if(result==null)
	 {
		 var imgurl="";
	 }	
	 else
	 {
		jQuery.post(ServletAddress+traceAdd+"HID="+Hid+"&UID="+uid,function(result){
		//alert(result);
		if(result=="Success")
		 {
			 //alert("成功跟踪");
		 }	
		 else
		 {
			alert("跟踪失败,未成功登陆");
		 }
		
		});
	 
		imgurl="images/Type/"+result.type+"-"+result.area+"平方米.jpg";				   	 
		jQuery(window.parent.frames["bottomFrame"].document).find("#info").append(jQuery("<tr><td style='font-family:微软雅黑' width='80'>"+"房屋号："+"</td><td width='171'>"+result.premise+"</td> </tr>"));	
		jQuery(window.parent.frames["bottomFrame"].document).find("#info").append(jQuery("<tr><td style='font-family:微软雅黑' width=80'>"+"位置："+"</td><td width='171'>"+result.location+"</td> </tr>"));	
		jQuery(window.parent.frames["bottomFrame"].document).find("#info").append(jQuery("<tr><td style='font-family:微软雅黑' width='80'>"+"价格："+"</td><td width='171'>"+result.housePrice+" 元"+"</td> </tr>"));	
		jQuery(window.parent.frames["bottomFrame"].document).find("#info").append(jQuery("<tr><td style='font-family:微软雅黑' width='80'>"+"面积："+"</td><td width='171'>"+result.area+" 平方米"+"</td> </tr>"));	
		jQuery(window.parent.frames["bottomFrame"].document).find("#info").append(jQuery("<tr><td style='font-family:微软雅黑' width='80'>"+"户型："+"</td><td width='171'>"+result.type+"</td> </tr>"));						
		jQuery(window.parent.frames["bottomFrame"].document).find("#imgStyle").attr("src",imgurl);	
		
		
		var layer3D=scene.get_layer3Ds().get_item(layerName);					
		if (layer3D != null) 
		{
			var feature3D = layer3D.findFeature3DByID(layer3D.getFieldValue(0)); 
			//var feature3D = layer3D.findFeature3DByID(43); 
			if(feature3D==null)
			{
				alert("feature3D为空");
			}	
			else
			{
				geometry = feature3D.get_geometry();	
				var geoCoord2d=geometry.get_innerPoint();	
				var geoCoord3d=new SuperMap.Web.Core.Point3D(geoCoord2d.x,geoCoord2d.y,0);
				var screenCoord=sceneControl.globeToPixel(geoCoord3d);							
			}			
		}				
	
		//弹出窗口
		jQuery(window.parent.document).find("#panel").css("left",screenCoord.x);
		jQuery(window.parent.document).find("#panel").css("top",screenCoord.y-200);				 
		jQuery(window.parent.document).find("#panel").show("slow");
		//收起
		jQuery(window.parent.document).find("#panel").animate({height:'250'},0);	
		pan_Method();
		sceneControl.removeEvent("objectSelected", handler);		
		
	 }
	 
							   
	});		
						   
		//获取鼠标位置
	
} 

function viewEntire()
{
	var newCamera=new SuperMap.Web.Realspace.Camera(119.3458,26.0475,50);
    scene.get_flyingOperator().flyTo(newCamera, 2000, SuperMap.Web.Realspace.FlyingMode.FLYHEADING);    
}

function HigherView()
{
	var newCamera=new SuperMap.Web.Realspace.Camera(119.345140933213,26.0490049555771,200);
    scene.get_flyingOperator().flyTo(newCamera, 2000, SuperMap.Web.Realspace.FlyingMode.FLYHEADING);    
}

function drawLine(geoLine3d)
{	
	//为创建的线填充颜色
	var feature = new SuperMap.Web.Core.Feature3D();
    feature.set_geometry(geoLine3d);	
	//添加要素显示风格
    var style = new SuperMap.Web.Core.Style3D();
	style.set_lineWidth(4);
    style.set_lineColor(new SuperMap.Web.Core.Color(255,0,0,255));
	feature.set_style3D(style);
	
	//将线要素填充到跟踪图层中
	
	scene.get_trackingLayer3D().add(feature,"A Line");
    scene.get_trackingLayer3D().set_isVisible(true);
    scene.refresh();
	
	//飞行的时候最好获取几何对象的外接矩形，然后飞行到外接矩形的位置
	var rectangle2d=geoLine3d.get_bounds();
	scene.get_flyingOperator().flyToBounds(rectangle2d,2000);
	
}






	
	
	
	  


